﻿using System.Linq;
using System.Threading.Tasks;
using Leo.Awesome.DictionaryManagement.Domain;
using Leo.Awesome.DictionaryManagement.Events;
using Volo.Abp.DependencyInjection;
using Volo.Abp.EventBus.Distributed;

namespace Leo.Awesome.DictionaryManagement.Handlers
{
    public class LockDictionItemsByIdsHandler : IDistributedEventHandler<LockDictionItemsByIdsEvent>, ITransientDependency
    {
        private readonly IDictionaryItemRepository dictionaryItemRepository;

        public LockDictionItemsByIdsHandler(IDictionaryItemRepository dictionaryItemRepository)
        {
            this.dictionaryItemRepository = dictionaryItemRepository;
        }
        public async Task HandleEventAsync(LockDictionItemsByIdsEvent eventData)
        {
            if (eventData == null || !eventData.Ids.Any())
            {
                return;
            }
            var ids = eventData.Ids;
            var dicitems = await dictionaryItemRepository.GetListAsync(x => ids.Contains(x.Id));
            foreach ( var dicitem in dicitems) 
            {
                dicitem.IsLocked = true;
                await dictionaryItemRepository.UpdateAsync(dicitem);
            }
        }
    }
}
